namespace SimpleWebServices.Models
{
    using System.Xml.XPath;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Security.Cryptography;
    using SimpleWebServices.Web;
    using Xunit;

    public sealed class FormSubmissionFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<FormSubmission>()
                .DerivesFrom<Resource>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("submission")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            FormSubmission actual = (
                "<submission>" +
                    "<data>" +
                        "<value name='foo'>bar</value>" +
                    "</data>" +
                "</submission>").Deserialize<FormSubmission>();

            Assert.NotNull(actual);

            Assert.Equal<string>("bar", actual.Data["foo"]);
        }

        [Fact]
        public void deserializeEmpty()
        {
            FormSubmission actual = "<submission />".Deserialize<FormSubmission>();

            Assert.NotNull(actual);

            Assert.Empty(actual.Data);
        }

        [Fact]
        public void serialize()
        {
            FormSubmission obj = new FormSubmission
            {
                Data = new DataCollection
                {
                    { "foo", "bar" }
                }
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/submission/data/value[@name='foo'][text()='bar'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new FormSubmission());
        }

        [Fact]
        public void prop_Entity()
        {
            Assert.True(new PropertyExpectations(typeof(FormSubmission).GetProperty("Entity"))
                .XmlIgnore()
                .TypeIs<string>()
                .DefaultValueIsNotNull()
                .Result);
        }

        [Fact]
        public void prop_Entity_get()
        {
            FormSubmission obj = new FormSubmission
            {
                Data = new DataCollection
                {
                    { "foo", "bar" }
                }
            };

            string expected = "Z1VxCVI+o62NoekvBPd54Q==";
            string actual = MD5Hash.Compute(obj.Entity);

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void prop_Entity_getEmpty()
        {
            string expected = string.Empty;
            string actual = new FormSubmission().Entity;

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_Store()
        {
            FakeRepository.Clear();

            try
            {
                FakeRepository sessions = FakeRepository.Register<FormSubmission>();

                (new FormSubmission
                {
                    Urn = "urn://example.com/results/" + Token.New(),
                    Data = new DataCollection
                    {
                        { "foo", "bar" }
                    }
                }).Store();

                Assert.True((bool)sessions.Xml.CreateNavigator().Evaluate("1=count(/repository/object[@token][@urn]/submission/data/value[@name='foo'][text()='bar'])"));
            }
            finally
            {
                FakeRepository.Reset();
            }
        }
    }
}